package sel.me.sharding.learning.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.support.TransactionTemplate;
import tk.mybatis.spring.annotation.MapperScan;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;

import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "sel.me.sharding.learning.dao",
		sqlSessionFactoryRef = "shardSqlSessionFactory")
public class MybatisConfig {


	@Bean(name = "shardSqlSessionFactory")
    public SqlSessionFactory shardSqlSessionFactory(@Qualifier("shardingDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
		sessionFactory.setDataSource(dataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource("classpath:spring/mybatis-config.xml"));
        return sessionFactory.getObject();
    }


//	@Bean(name = "normalSqlSessionFactory")
//    public SqlSessionFactory normalSqlSessionFactory(@Qualifier("ds0") DataSource dataSource) throws Exception {
//        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
//		sessionFactory.setDataSource(dataSource);
//        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
//        sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource("classpath:spring/mybatis-config.xml"));
////        /** 增加分页拦截器 , performanceInterceptor()*/
////        sessionFactory.setPlugins(new Interceptor[]{new PaginationInterceptor()});
//        return sessionFactory.getObject();
//    }


    @Bean(name = "shardTransactionManager")
    public DataSourceTransactionManager shardTransactionManager(@Qualifier("shardingDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

//    @Bean(name = "shardSqlSessionTemplate")
//    public SqlSessionTemplate shardSqlSessionTemplate(@Qualifier("shardSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
//        return new SqlSessionTemplate(sqlSessionFactory);
//    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
    	MapperScannerConfigurer m = new MapperScannerConfigurer();
    	m.setSqlSessionFactoryBeanName("shardSqlSessionFactory");
    	m.setBasePackage("sel.me.sharding.learning.dao");
    	return m;
    }


    @Bean
	public TransactionTemplate transactionTemplate(@Qualifier("shardTransactionManager") PlatformTransactionManager transactionManager) {
    	TransactionTemplate t = new TransactionTemplate();
    	t.setTransactionManager(transactionManager);
    	return t;
	}
    
}
